package com.google.android.exoplayer2.audio;

import android.annotation.TargetApi;
import android.media.AudioAttributes;
import android.media.AudioAttributes.Builder;
import android.media.AudioFormat;
import android.media.AudioTrack;
import android.os.ConditionVariable;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.audio.AudioSink.ConfigurationException;
import com.google.android.exoplayer2.audio.AudioSink.InitializationException;
import com.google.android.exoplayer2.audio.AudioSink.WriteException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import o.abq;
import o.aby;
import o.abz;
import o.ach;
import o.acj;
import o.acl;
import o.acm;
import o.acn;
import o.acp;
import o.acq;
import o.acr;
import o.acu;
import o.amm;
import o.amz;
import o.anr;

public final class DefaultAudioSink implements AudioSink {
    public static boolean ˊ = false;
    public static boolean ˏ = false;
    private final AudioProcessor[] ʻ;
    private int ʻॱ;
    private final AudioProcessor[] ʼ;
    private boolean ʼॱ;
    private final acp ʽ;
    private int ʽॱ;
    private int ʾ;
    private boolean ʿ;
    private aby ˈ;
    private long ˉ;
    private long ˊˊ;
    @Nullable
    private abq ˊˋ;
    private AudioTrack ˊॱ;
    @Nullable
    private ByteBuffer ˊᐝ;
    private final b ˋ;
    private abq ˋˊ;
    private int ˋˋ;
    private final ArrayDeque<a> ˋॱ;
    private int ˋᐝ;
    private int ˌ;
    private long ˍ;
    private final boolean ˎ;
    private long ˎˎ;
    private int ˎˏ;
    private int ˏˎ;
    private long ˏˏ;
    @Nullable
    private AudioTrack ˏॱ;
    private long ˑ;
    private final ach ͺ;
    private long ͺॱ;
    @Nullable
    private ByteBuffer ـ;
    @Nullable
    private final abz ॱ;
    private AudioProcessor[] ॱʻ;
    @Nullable
    private ByteBuffer ॱʼ;
    private float ॱʽ;
    @Nullable
    private com.google.android.exoplayer2.audio.AudioSink.d ॱˊ;
    private int ॱˋ;
    private boolean ॱˎ;
    private ByteBuffer[] ॱͺ;
    private final acu ॱॱ;
    private int ॱᐝ;
    private final ConditionVariable ᐝ;
    private byte[] ᐝˊ;
    private int ᐝˋ;
    private boolean ᐝॱ;
    private boolean ᐝᐝ;
    private int ᐧ;
    private boolean ᐨ;
    private int ᶥ;
    private acj ㆍ;
    private boolean ꓸ;
    private long ꜟ;

    public static final class InvalidAudioTrackTimestampException extends RuntimeException {
        private InvalidAudioTrackTimestampException(String str) {
            super(str);
        }
    }

    static final class a {
        private final long ˊ;
        private final abq ˎ;
        private final long ˏ;

        private a(abq o_abq, long j, long j2) {
            this.ˎ = o_abq;
            this.ˊ = j;
            this.ˏ = j2;
        }
    }

    public interface b {
        abq ˊ(abq o_abq);

        long ˎ();

        AudioProcessor[] ˏ();

        long ॱ(long j);
    }

    public static class d implements b {
        private final acr ˋ = new acr();
        private final acq ˎ = new acq();
        private final AudioProcessor[] ॱ;

        public d(AudioProcessor... audioProcessorArr) {
            this.ॱ = (AudioProcessor[]) Arrays.copyOf(audioProcessorArr, audioProcessorArr.length + 2);
            this.ॱ[audioProcessorArr.length] = this.ˎ;
            this.ॱ[audioProcessorArr.length + 1] = this.ˋ;
        }

        public AudioProcessor[] ˏ() {
            return this.ॱ;
        }

        public abq ˊ(abq o_abq) {
            this.ˎ.ˎ(o_abq.ˏ);
            return new abq(this.ˋ.ॱ(o_abq.ॱ), this.ˋ.ˋ(o_abq.ˋ), o_abq.ˏ);
        }

        public long ॱ(long j) {
            return this.ˋ.ˋ(j);
        }

        public long ˎ() {
            return this.ˎ.ॱॱ();
        }
    }

    final class e implements o.ach.d {
        final /* synthetic */ DefaultAudioSink ˏ;

        private e(DefaultAudioSink defaultAudioSink) {
            this.ˏ = defaultAudioSink;
        }

        public void ˋ(long j, long j2, long j3, long j4) {
            String str = "Spurious audio timestamp (frame position mismatch): " + j + ", " + j2 + ", " + j3 + ", " + j4 + ", " + this.ˏ.ॱˎ() + ", " + this.ˏ.ॱˋ();
            if (DefaultAudioSink.ˏ) {
                throw new InvalidAudioTrackTimestampException(str);
            }
            amz.ˏ("AudioTrack", str);
        }

        public void ॱ(long j, long j2, long j3, long j4) {
            String str = "Spurious audio timestamp (system clock mismatch): " + j + ", " + j2 + ", " + j3 + ", " + j4 + ", " + this.ˏ.ॱˎ() + ", " + this.ˏ.ॱˋ();
            if (DefaultAudioSink.ˏ) {
                throw new InvalidAudioTrackTimestampException(str);
            }
            amz.ˏ("AudioTrack", str);
        }

        public void ˏ(long j) {
            amz.ˏ("AudioTrack", "Ignoring impossibly large audio latency: " + j);
        }

        public void ॱ(int i, long j) {
            if (this.ˏ.ॱˊ != null) {
                this.ˏ.ॱˊ.ॱ(i, j, SystemClock.elapsedRealtime() - this.ˏ.ꜟ);
            }
        }
    }

    public DefaultAudioSink(@Nullable abz o_abz, AudioProcessor[] audioProcessorArr) {
        this(o_abz, audioProcessorArr, false);
    }

    public DefaultAudioSink(@Nullable abz o_abz, AudioProcessor[] audioProcessorArr, boolean z) {
        this(o_abz, new d(audioProcessorArr), z);
    }

    public DefaultAudioSink(@Nullable abz o_abz, b bVar, boolean z) {
        this.ॱ = o_abz;
        this.ˋ = (b) amm.ॱ(bVar);
        this.ˎ = z;
        this.ᐝ = new ConditionVariable(true);
        this.ͺ = new ach(new e());
        this.ʽ = new acp();
        this.ॱॱ = new acu();
        ArrayList arrayList = new ArrayList();
        Collections.addAll(arrayList, new AudioProcessor[]{new acl(), this.ʽ, this.ॱॱ});
        Collections.addAll(arrayList, bVar.ˏ());
        this.ʻ = (AudioProcessor[]) arrayList.toArray(new AudioProcessor[arrayList.size()]);
        this.ʼ = new AudioProcessor[]{new acm()};
        this.ॱʽ = 1.0f;
        this.ˏˎ = 0;
        this.ˈ = aby.ˏ;
        this.ᶥ = 0;
        this.ㆍ = new acj(0, 0.0f);
        this.ˋˊ = abq.ˎ;
        this.ᐧ = -1;
        this.ॱʻ = new AudioProcessor[0];
        this.ॱͺ = new ByteBuffer[0];
        this.ˋॱ = new ArrayDeque();
    }

    public void ˎ(com.google.android.exoplayer2.audio.AudioSink.d dVar) {
        this.ॱˊ = dVar;
    }

    public boolean ˎ(int i, int i2) {
        boolean z = true;
        if (!anr.ˋ(i2)) {
            if (this.ॱ == null || !this.ॱ.ˋ(i2) || (i != -1 && i > this.ॱ.ˏ())) {
                z = false;
            }
            return z;
        } else if (i2 != 4 || anr.ˋ >= 21) {
            return true;
        } else {
            return false;
        }
    }

    public long ˏ(boolean z) {
        if (!ॱᐝ() || this.ˏˎ == 0) {
            return Long.MIN_VALUE;
        }
        long min = Math.min(this.ͺ.ˋ(z), ॱ(ॱˋ()));
        return ˏ(ˊ(min)) + this.ˏˏ;
    }

    public void ˎ(int i, int i2, int i3, int i4, @Nullable int[] iArr, int i5, int i6) throws ConfigurationException {
        int i7;
        int i8;
        int i9;
        this.ॱˋ = i3;
        this.ॱˎ = anr.ˋ(i);
        boolean z = this.ˎ && ˎ(i2, 4) && anr.ॱ(i);
        this.ᐝॱ = z;
        if (this.ॱˎ) {
            this.ˌ = anr.ˊ(i, i2);
        }
        boolean z2 = this.ॱˎ && i != 4;
        z = z2 && !this.ᐝॱ;
        this.ʿ = z;
        if (anr.ˋ < 21 && i2 == 8 && iArr == null) {
            iArr = new int[6];
            for (i7 = 0; i7 < iArr.length; i7++) {
                iArr[i7] = i7;
            }
        }
        int i10;
        int i11;
        if (z2) {
            this.ॱॱ.ˋ(i5, i6);
            this.ʽ.ˏ(iArr);
            AudioProcessor[] ʽॱ = ʽॱ();
            int length = ʽॱ.length;
            i8 = 0;
            i9 = i2;
            i10 = 0;
            i7 = i;
            i11 = i3;
            while (i8 < length) {
                AudioProcessor audioProcessor = ʽॱ[i8];
                try {
                    int ˋ = audioProcessor.ˋ(i11, i9, i7) | i10;
                    if (audioProcessor.ˏ()) {
                        i9 = audioProcessor.ˋ();
                        i11 = audioProcessor.ˎ();
                        i7 = audioProcessor.ˊ();
                    }
                    i8++;
                    i10 = ˋ;
                } catch (Throwable e) {
                    throw new ConfigurationException(e);
                }
            }
        }
        i7 = i;
        i9 = i2;
        i10 = 0;
        i11 = i3;
        i8 = ˏ(i9, this.ॱˎ);
        if (i8 == 0) {
            throw new ConfigurationException("Unsupported channel count: " + i9);
        } else if (i10 != 0 || !ॱᐝ() || this.ʾ != i7 || this.ʻॱ != i11 || this.ॱᐝ != i8) {
            ᐝ();
            this.ʼॱ = z2;
            this.ʻॱ = i11;
            this.ॱᐝ = i8;
            this.ʾ = i7;
            this.ˋᐝ = this.ॱˎ ? anr.ˊ(this.ʾ, i9) : -1;
            if (i4 == 0) {
                i4 = ͺ();
            }
            this.ʽॱ = i4;
        }
    }

    private int ͺ() {
        if (this.ॱˎ) {
            int minBufferSize = AudioTrack.getMinBufferSize(this.ʻॱ, this.ॱᐝ, this.ʾ);
            amm.ˏ(minBufferSize != -2);
            return anr.ॱ(minBufferSize * 4, ((int) ʻ(250000)) * this.ˋᐝ, (int) Math.max((long) minBufferSize, ʻ(750000) * ((long) this.ˋᐝ)));
        }
        int ˊ = ˊ(this.ʾ);
        if (this.ʾ == 5) {
            ˊ *= 2;
        }
        return (int) ((((long) ˊ) * 250000) / 1000000);
    }

    private void ˊॱ() {
        ArrayList arrayList = new ArrayList();
        for (AudioProcessor audioProcessor : ʽॱ()) {
            if (audioProcessor.ˏ()) {
                arrayList.add(audioProcessor);
            } else {
                audioProcessor.ʼ();
            }
        }
        int size = arrayList.size();
        this.ॱʻ = (AudioProcessor[]) arrayList.toArray(new AudioProcessor[size]);
        this.ॱͺ = new ByteBuffer[size];
        ˋॱ();
    }

    private void ˋॱ() {
        for (int i = 0; i < this.ॱʻ.length; i++) {
            AudioProcessor audioProcessor = this.ॱʻ[i];
            audioProcessor.ʼ();
            this.ॱͺ[i] = audioProcessor.ʽ();
        }
    }

    private void ˏॱ() throws InitializationException {
        this.ᐝ.block();
        this.ˊॱ = ˈ();
        int audioSessionId = this.ˊॱ.getAudioSessionId();
        if (ˊ && anr.ˋ < 21) {
            if (!(this.ˏॱ == null || audioSessionId == this.ˏॱ.getAudioSessionId())) {
                ʻॱ();
            }
            if (this.ˏॱ == null) {
                this.ˏॱ = ˎ(audioSessionId);
            }
        }
        if (this.ᶥ != audioSessionId) {
            this.ᶥ = audioSessionId;
            if (this.ॱˊ != null) {
                this.ॱˊ.ˊ(audioSessionId);
            }
        }
        this.ˋˊ = this.ʿ ? this.ˋ.ˊ(this.ˋˊ) : abq.ˎ;
        ˊॱ();
        this.ͺ.ˋ(this.ˊॱ, this.ʾ, this.ˋᐝ, this.ʽॱ);
        ᐝॱ();
        if (this.ㆍ.ˋ != 0) {
            this.ˊॱ.attachAuxEffect(this.ㆍ.ˋ);
            this.ˊॱ.setAuxEffectSendLevel(this.ㆍ.ˎ);
        }
    }

    public void ˏ() {
        this.ᐝᐝ = true;
        if (ॱᐝ()) {
            this.ͺ.ˏ();
            this.ˊॱ.play();
        }
    }

    public void ॱ() {
        if (this.ˏˎ == 1) {
            this.ˏˎ = 2;
        }
    }

    public boolean ॱ(ByteBuffer byteBuffer, long j) throws InitializationException, WriteException {
        boolean z = this.ॱʼ == null || byteBuffer == this.ॱʼ;
        amm.ˊ(z);
        if (!ॱᐝ()) {
            ˏॱ();
            if (this.ᐝᐝ) {
                ˏ();
            }
        }
        if (!this.ͺ.ˏ(ॱˋ())) {
            return false;
        }
        if (this.ॱʼ == null) {
            if (!byteBuffer.hasRemaining()) {
                return true;
            }
            if (!this.ॱˎ && this.ˎˏ == 0) {
                this.ˎˏ = ॱ(this.ʾ, byteBuffer);
                if (this.ˎˏ == 0) {
                    return true;
                }
            }
            if (this.ˊˋ != null) {
                if (!ॱˊ()) {
                    return false;
                }
                abq o_abq = this.ˊˋ;
                this.ˊˋ = null;
                this.ˋॱ.add(new a(this.ˋ.ˊ(o_abq), Math.max(0, j), ॱ(ॱˋ())));
                ˊॱ();
            }
            if (this.ˏˎ == 0) {
                this.ˏˏ = Math.max(0, j);
                this.ˏˎ = 1;
            } else {
                long ˎ = this.ˏˏ + ˎ(ॱˎ() - this.ॱॱ.ˋॱ());
                if (this.ˏˎ == 1 && Math.abs(ˎ - j) > 200000) {
                    amz.ˋ("AudioTrack", "Discontinuity detected [expected " + ˎ + ", got " + j + "]");
                    this.ˏˎ = 2;
                }
                if (this.ˏˎ == 2) {
                    ˎ = j - ˎ;
                    this.ˏˏ += ˎ;
                    this.ˏˎ = 1;
                    if (!(this.ॱˊ == null || ˎ == 0)) {
                        this.ॱˊ.ॱ();
                    }
                }
            }
            if (this.ॱˎ) {
                this.ˍ += (long) byteBuffer.remaining();
            } else {
                this.ˎˎ += (long) this.ˎˏ;
            }
            this.ॱʼ = byteBuffer;
        }
        if (this.ʼॱ) {
            ˋ(j);
        } else {
            ˊ(this.ॱʼ, j);
        }
        if (!this.ॱʼ.hasRemaining()) {
            this.ॱʼ = null;
            return true;
        } else if (!this.ͺ.ॱ(ॱˋ())) {
            return false;
        } else {
            amz.ˏ("AudioTrack", "Resetting stalled audio track");
            ᐝ();
            return true;
        }
    }

    private void ˋ(long j) throws WriteException {
        int length = this.ॱʻ.length;
        int i = length;
        while (i >= 0) {
            ByteBuffer byteBuffer = i > 0 ? this.ॱͺ[i - 1] : this.ॱʼ != null ? this.ॱʼ : AudioProcessor.ˏ;
            if (i == length) {
                ˊ(byteBuffer, j);
            } else {
                AudioProcessor audioProcessor = this.ॱʻ[i];
                audioProcessor.ˎ(byteBuffer);
                ByteBuffer ʽ = audioProcessor.ʽ();
                this.ॱͺ[i] = ʽ;
                if (ʽ.hasRemaining()) {
                    i++;
                }
            }
            if (!byteBuffer.hasRemaining()) {
                i--;
            } else {
                return;
            }
        }
    }

    private void ˊ(ByteBuffer byteBuffer, long j) throws WriteException {
        boolean z = true;
        int i = 0;
        if (byteBuffer.hasRemaining()) {
            int remaining;
            int position;
            if (this.ـ != null) {
                boolean z2;
                if (this.ـ == byteBuffer) {
                    z2 = true;
                } else {
                    z2 = false;
                }
                amm.ˊ(z2);
            } else {
                this.ـ = byteBuffer;
                if (anr.ˋ < 21) {
                    remaining = byteBuffer.remaining();
                    if (this.ᐝˊ == null || this.ᐝˊ.length < remaining) {
                        this.ᐝˊ = new byte[remaining];
                    }
                    position = byteBuffer.position();
                    byteBuffer.get(this.ᐝˊ, 0, remaining);
                    byteBuffer.position(position);
                    this.ᐝˋ = 0;
                }
            }
            position = byteBuffer.remaining();
            if (anr.ˋ < 21) {
                remaining = this.ͺ.ˎ(this.ˑ);
                if (remaining > 0) {
                    i = this.ˊॱ.write(this.ᐝˊ, this.ᐝˋ, Math.min(position, remaining));
                    if (i > 0) {
                        this.ᐝˋ += i;
                        byteBuffer.position(byteBuffer.position() + i);
                    }
                }
            } else if (this.ꓸ) {
                if (j == -9223372036854775807L) {
                    z = false;
                }
                amm.ˏ(z);
                i = ॱ(this.ˊॱ, byteBuffer, position, j);
            } else {
                i = ˊ(this.ˊॱ, byteBuffer, position);
            }
            this.ꜟ = SystemClock.elapsedRealtime();
            if (i < 0) {
                throw new WriteException(i);
            }
            if (this.ॱˎ) {
                this.ˑ += (long) i;
            }
            if (i == position) {
                if (!this.ॱˎ) {
                    this.ͺॱ += (long) this.ˎˏ;
                }
                this.ـ = null;
            }
        }
    }

    public void ˎ() throws WriteException {
        if (!this.ᐨ && ॱᐝ() && ॱˊ()) {
            this.ͺ.ˋ(ॱˋ());
            this.ˊॱ.stop();
            this.ˋˋ = 0;
            this.ᐨ = true;
        }
    }

    private boolean ॱˊ() throws WriteException {
        boolean z;
        if (this.ᐧ == -1) {
            int i;
            if (this.ʼॱ) {
                i = 0;
            } else {
                i = this.ॱʻ.length;
            }
            this.ᐧ = i;
            z = true;
        } else {
            z = false;
        }
        while (this.ᐧ < this.ॱʻ.length) {
            AudioProcessor audioProcessor = this.ॱʻ[this.ᐧ];
            if (z) {
                audioProcessor.ॱ();
            }
            ˋ(-9223372036854775807L);
            if (!audioProcessor.ʻ()) {
                return false;
            }
            this.ᐧ++;
            z = true;
        }
        if (this.ـ != null) {
            ˊ(this.ـ, -9223372036854775807L);
            if (this.ـ != null) {
                return false;
            }
        }
        this.ᐧ = -1;
        return true;
    }

    public boolean ˋ() {
        return !ॱᐝ() || (this.ᐨ && !ˊ());
    }

    public boolean ˊ() {
        return ॱᐝ() && this.ͺ.ˊ(ॱˋ());
    }

    public abq ॱ(abq o_abq) {
        if (!ॱᐝ() || this.ʿ) {
            Object ˊ = this.ˊˋ != null ? this.ˊˋ : !this.ˋॱ.isEmpty() ? ((a) this.ˋॱ.getLast()).ˎ : this.ˋˊ;
            if (!o_abq.equals(ˊ)) {
                if (ॱᐝ()) {
                    this.ˊˋ = o_abq;
                } else {
                    this.ˋˊ = this.ˋ.ˊ(o_abq);
                }
            }
            return this.ˋˊ;
        }
        this.ˋˊ = abq.ˎ;
        return this.ˋˊ;
    }

    public abq ʻ() {
        return this.ˋˊ;
    }

    public void ˋ(aby o_aby) {
        if (!this.ˈ.equals(o_aby)) {
            this.ˈ = o_aby;
            if (!this.ꓸ) {
                ᐝ();
                this.ᶥ = 0;
            }
        }
    }

    public void ˏ(acj o_acj) {
        if (!this.ㆍ.equals(o_acj)) {
            int i = o_acj.ˋ;
            float f = o_acj.ˎ;
            if (this.ˊॱ != null) {
                if (this.ㆍ.ˋ != i) {
                    this.ˊॱ.attachAuxEffect(i);
                }
                if (i != 0) {
                    this.ˊॱ.setAuxEffectSendLevel(f);
                }
            }
            this.ㆍ = o_acj;
        }
    }

    public void ˋ(int i) {
        amm.ˏ(anr.ˋ >= 21);
        if (!this.ꓸ || this.ᶥ != i) {
            this.ꓸ = true;
            this.ᶥ = i;
            ᐝ();
        }
    }

    public void ॱॱ() {
        if (this.ꓸ) {
            this.ꓸ = false;
            this.ᶥ = 0;
            ᐝ();
        }
    }

    public void ˋ(float f) {
        if (this.ॱʽ != f) {
            this.ॱʽ = f;
            ᐝॱ();
        }
    }

    private void ᐝॱ() {
        if (!ॱᐝ()) {
            return;
        }
        if (anr.ˋ >= 21) {
            ˋ(this.ˊॱ, this.ॱʽ);
        } else {
            ˏ(this.ˊॱ, this.ॱʽ);
        }
    }

    public void ʽ() {
        this.ᐝᐝ = false;
        if (ॱᐝ() && this.ͺ.ˊ()) {
            this.ˊॱ.pause();
        }
    }

    public void ᐝ() {
        if (ॱᐝ()) {
            this.ˍ = 0;
            this.ˎˎ = 0;
            this.ˑ = 0;
            this.ͺॱ = 0;
            this.ˎˏ = 0;
            if (this.ˊˋ != null) {
                this.ˋˊ = this.ˊˋ;
                this.ˊˋ = null;
            } else if (!this.ˋॱ.isEmpty()) {
                this.ˋˊ = ((a) this.ˋॱ.getLast()).ˎ;
            }
            this.ˋॱ.clear();
            this.ˊˊ = 0;
            this.ˉ = 0;
            this.ॱॱ.ॱॱ();
            this.ॱʼ = null;
            this.ـ = null;
            ˋॱ();
            this.ᐨ = false;
            this.ᐧ = -1;
            this.ˊᐝ = null;
            this.ˋˋ = 0;
            this.ˏˎ = 0;
            if (this.ͺ.ˋ()) {
                this.ˊॱ.pause();
            }
            final AudioTrack audioTrack = this.ˊॱ;
            this.ˊॱ = null;
            this.ͺ.ॱ();
            this.ᐝ.close();
            new Thread(this) {
                final /* synthetic */ DefaultAudioSink ˎ;

                public void run() {
                    try {
                        audioTrack.flush();
                        audioTrack.release();
                    } finally {
                        this.ˎ.ᐝ.open();
                    }
                }
            }.start();
        }
    }

    public void ʼ() {
        ᐝ();
        ʻॱ();
        for (AudioProcessor ᐝ : this.ʻ) {
            ᐝ.ᐝ();
        }
        for (AudioProcessor ᐝ2 : this.ʼ) {
            ᐝ2.ᐝ();
        }
        this.ᶥ = 0;
        this.ᐝᐝ = false;
    }

    private void ʻॱ() {
        if (this.ˏॱ != null) {
            final AudioTrack audioTrack = this.ˏॱ;
            this.ˏॱ = null;
            new Thread(this) {
                final /* synthetic */ DefaultAudioSink ˊ;

                public void run() {
                    audioTrack.release();
                }
            }.start();
        }
    }

    private long ˊ(long j) {
        a aVar = null;
        while (!this.ˋॱ.isEmpty() && j >= ((a) this.ˋॱ.getFirst()).ˏ) {
            aVar = (a) this.ˋॱ.remove();
        }
        if (aVar != null) {
            this.ˋˊ = aVar.ˎ;
            this.ˉ = aVar.ˏ;
            this.ˊˊ = aVar.ˊ - this.ˏˏ;
        }
        if (this.ˋˊ.ॱ == 1.0f) {
            return (this.ˊˊ + j) - this.ˉ;
        }
        if (this.ˋॱ.isEmpty()) {
            return this.ˊˊ + this.ˋ.ॱ(j - this.ˉ);
        }
        return this.ˊˊ + anr.ˏ(j - this.ˉ, this.ˋˊ.ॱ);
    }

    private long ˏ(long j) {
        return ॱ(this.ˋ.ˎ()) + j;
    }

    private boolean ॱᐝ() {
        return this.ˊॱ != null;
    }

    private long ˎ(long j) {
        return (1000000 * j) / ((long) this.ॱˋ);
    }

    private long ॱ(long j) {
        return (1000000 * j) / ((long) this.ʻॱ);
    }

    private long ʻ(long j) {
        return (((long) this.ʻॱ) * j) / 1000000;
    }

    private long ॱˎ() {
        return this.ॱˎ ? this.ˍ / ((long) this.ˌ) : this.ˎˎ;
    }

    private long ॱˋ() {
        return this.ॱˎ ? this.ˑ / ((long) this.ˋᐝ) : this.ͺॱ;
    }

    private AudioTrack ˈ() throws InitializationException {
        AudioTrack ʼॱ;
        int ʻ;
        if (anr.ˋ >= 21) {
            ʼॱ = ʼॱ();
        } else {
            ʻ = anr.ʻ(this.ˈ.ˋ);
            if (this.ᶥ == 0) {
                ʼॱ = new AudioTrack(ʻ, this.ʻॱ, this.ॱᐝ, this.ʾ, this.ʽॱ, 1);
            } else {
                ʼॱ = new AudioTrack(ʻ, this.ʻॱ, this.ॱᐝ, this.ʾ, this.ʽॱ, 1, this.ᶥ);
            }
        }
        ʻ = ʼॱ.getState();
        if (ʻ == 1) {
            return ʼॱ;
        }
        try {
            ʼॱ.release();
        } catch (Exception e) {
        }
        throw new InitializationException(ʻ, this.ʻॱ, this.ॱᐝ, this.ʽॱ);
    }

    @TargetApi(21)
    private AudioTrack ʼॱ() {
        AudioAttributes build;
        if (this.ꓸ) {
            build = new Builder().setContentType(3).setFlags(16).setUsage(1).build();
        } else {
            build = this.ˈ.ˏ();
        }
        return new AudioTrack(build, new AudioFormat.Builder().setChannelMask(this.ॱᐝ).setEncoding(this.ʾ).setSampleRate(this.ʻॱ).build(), this.ʽॱ, 1, this.ᶥ != 0 ? this.ᶥ : 0);
    }

    private AudioTrack ˎ(int i) {
        return new AudioTrack(3, 4000, 4, 2, 2, 0, i);
    }

    private AudioProcessor[] ʽॱ() {
        return this.ᐝॱ ? this.ʼ : this.ʻ;
    }

    private static int ˏ(int i, boolean z) {
        if (anr.ˋ <= 28 && !z) {
            if (i == 7) {
                i = 8;
            } else if (i == 3 || i == 4 || i == 5) {
                i = 6;
            }
        }
        if (anr.ˋ <= 26 && "fugu".equals(anr.ˎ) && !z && r2 == 1) {
            i = 2;
        }
        return anr.ˏ(i);
    }

    private static int ˊ(int i) {
        switch (i) {
            case 5:
                return 80000;
            case 6:
                return 768000;
            case 7:
                return 192000;
            case 8:
                return 2250000;
            case 14:
                return 3062500;
            default:
                throw new IllegalArgumentException();
        }
    }

    private static int ॱ(int i, ByteBuffer byteBuffer) {
        if (i == 7 || i == 8) {
            return acn.ˎ(byteBuffer);
        }
        if (i == 5) {
            return Ac3Util.ˏ();
        }
        if (i == 6) {
            return Ac3Util.ॱ(byteBuffer);
        }
        if (i == 14) {
            int ˎ = Ac3Util.ˎ(byteBuffer);
            if (ˎ == -1) {
                return 0;
            }
            return Ac3Util.ˏ(byteBuffer, ˎ) * 16;
        }
        throw new IllegalStateException("Unexpected audio encoding: " + i);
    }

    @TargetApi(21)
    private static int ˊ(AudioTrack audioTrack, ByteBuffer byteBuffer, int i) {
        return audioTrack.write(byteBuffer, i, 1);
    }

    @TargetApi(21)
    private int ॱ(AudioTrack audioTrack, ByteBuffer byteBuffer, int i, long j) {
        int write;
        if (this.ˊᐝ == null) {
            this.ˊᐝ = ByteBuffer.allocate(16);
            this.ˊᐝ.order(ByteOrder.BIG_ENDIAN);
            this.ˊᐝ.putInt(1431633921);
        }
        if (this.ˋˋ == 0) {
            this.ˊᐝ.putInt(4, i);
            this.ˊᐝ.putLong(8, 1000 * j);
            this.ˊᐝ.position(0);
            this.ˋˋ = i;
        }
        int remaining = this.ˊᐝ.remaining();
        if (remaining > 0) {
            write = audioTrack.write(this.ˊᐝ, remaining, 1);
            if (write < 0) {
                this.ˋˋ = 0;
                return write;
            } else if (write < remaining) {
                return 0;
            }
        }
        write = ˊ(audioTrack, byteBuffer, i);
        if (write < 0) {
            this.ˋˋ = 0;
            return write;
        }
        this.ˋˋ -= write;
        return write;
    }

    @TargetApi(21)
    private static void ˋ(AudioTrack audioTrack, float f) {
        audioTrack.setVolume(f);
    }

    private static void ˏ(AudioTrack audioTrack, float f) {
        audioTrack.setStereoVolume(f, f);
    }
}
